Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | /** * API Route: Generate Flowchart Worksheet PDF * * Creates a printable worksheet with problems from the flowchart, * distributed by difficulty tier, with an optional answer key. */ import { NextResponse } from 'next/server' import { withAuth } from '@/lib/auth/withAuth' import { generateWorksheetPDF, type WorksheetConfig } from '@/lib/flowcharts/worksheet-generator' interface WorksheetRequest { /** Distribution of problems by difficulty tier */ distribution: { easy: number medium: number hard: number } /** Total number of problems */ problemCount: number /** Number of pages */ pageCount: number /** Whether to include an answer key */ includeAnswerKey?: boolean /** Optional custom title */ title?: string /** Whether to order problems by progressive difficulty (easy → medium → hard) */ orderByDifficulty?: boolean } export const POST = withAuth(async (request, { params }) => { try { const { id: flowchartId } = (await params) as { id: string } const body: WorksheetRequest = await request.json() // Validate required fields if (!body.distribution) { return NextResponse.json({ error: 'Missing required field: distribution' }, { status: 400 }) } if (!body.problemCount || body.problemCount < 1) { return NextResponse.json({ error: 'problemCount must be at least 1' }, { status: 400 }) } if (!body.pageCount || body.pageCount < 1) { return NextResponse.json({ error: 'pageCount must be at least 1' }, { status: 400 }) } // Validate distribution sums to 100 const distSum = body.distribution.easy + body.distribution.medium + body.distribution.hard if (Math.abs(distSum - 100) > 1) { return NextResponse.json( { error: `Distribution must sum to 100 (got ${distSum})` }, { status: 400 } ) } // Build config const config: WorksheetConfig = { distribution: body.distribution, problemCount: body.problemCount, pageCount: body.pageCount, includeAnswerKey: body.includeAnswerKey !== false, title: body.title, orderByDifficulty: body.orderByDifficulty ?? false, } // Generate PDF const pdfBuffer = await generateWorksheetPDF(flowchartId, config) // Return PDF return new NextResponse(new Uint8Array(pdfBuffer), { headers: { 'Content-Type': 'application/pdf', 'Content-Disposition': `attachment; filename="worksheet.pdf"`, }, }) } catch (error) { console.error('Worksheet generation error:', error) if (error instanceof Error && error.message.includes('not found')) { return NextResponse.json({ error: error.message }, { status: 404 }) } return NextResponse.json( { error: 'Failed to generate worksheet', details: error instanceof Error ? error.message : 'Unknown error', }, { status: 500 } ) } }) |